package com.ycrlkj.android.modulescore.main.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.ycrlkj.android.modulesbase.libbase.utils.Logger;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiExceptionHandler;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import com.ycrlkj.android.modulescore.main.apiservice.VehicleApiService;
import com.ycrlkj.android.modulescore.main.databinding.ActivityVehicleDetailBinding;
import com.ycrlkj.android.modulescore.main.entity.Vehicle;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.functions.Consumer;

@AndroidEntryPoint
@Route(path = RoutePath.PAGE_VEHICLE_DETAIL)
public class VehicleDetailActivity extends AppCompatActivity {

    private ActivityVehicleDetailBinding binding;

    @Inject
    VehicleApiService vehicleApiService;

    @Autowired
    String id;

    @Autowired
    boolean editEnabled;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);

        if (id == null && id.length() != 19) {
            XPopupUtil.showAlert(this, "未找到车辆信息, 关闭页面", new OnConfirmListener() {
                @Override
                public void onConfirm() {
                    VehicleDetailActivity.this.finish();
                }
            });
        }

        binding = ActivityVehicleDetailBinding.inflate(getLayoutInflater());
        binding.setLifecycleOwner(this);

        setContentView(binding.getRoot());

        initTitleBar();
        get();
    }

    public void initTitleBar() {
        MTitleBar mTitleBar = binding.mTitleBar;

        //editEnabled 设置标题栏右侧按钮是否显示(只有从车辆列表页面点击item跳转来的才显示 修改按钮, 其他业务块跳转来到此页面的不显示修改按钮)
        mTitleBar.setBtnRightVisibility(editEnabled);

        mTitleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                VehicleDetailActivity.this.finish();
            }

            @Override
            public void setBtnRightOnClickListener() {
                ARouter.getInstance().build(RoutePath.PAGE_VEHICLE_EDIT).withString("id", id).navigation();
            }
        });
    }


    @SuppressLint("CheckResult")
    public void get() {
        vehicleApiService.getById(id)
                .compose(ResponseTransformer.obtain())
                .subscribe(vehicle -> {
                    binding.setVehicle(vehicle);
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException e) {
                        ApiExceptionHandler.doWith(VehicleDetailActivity.this, e);
                    }
                });
    }

}